//
//  HeadCellView.swift
//  微博
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class HeadCellView: UIView {
    
    //设置底部的约束
    var bottomConstraints : Constraint?
    var status:Status? {
        didSet{
            iconView.sd_setImageWithURL(status?.user?.profile_image)
            nameLabel.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrankImage
            verified_type_image.image = status?.user?.verified_typeImage
            //TODO: 时间和source
//            print(status?.source?.linkAndText().text)
            timeLabel.text = NSDate.stringToDate(status?.created_at ?? "")?.dateToStr()
            sourceBtn.setTitle(status?.source?.linkAndText().text ?? "", forState: .Normal)
            contentLabel.attributedText = EmoticonManager.sharedEmoticonManager.statuseTextToImageText(status?.text ?? "", font: contentLabel.font)
            
            
            //设置图片的url
            pictureView.urls = status?.picURLs
            //判断url是否有值
            //先卸载当前的约束
            self.bottomConstraints?.uninstall()
            if let urls = status?.picURLs where urls.count > 0 {
                //有配图的设置
                self.snp_updateConstraints { (make) -> Void in
                    //记录底部的约束
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                }
                
            }else{
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
        }
    }
    
    
    @objc func didSourceBtnClick(sender:UIButton) {
        if sender.titleLabel?.text == nil {
            return
        }
        let sourseWVC = SourseWebViewController()
        sourseWVC.urlStr = status?.source?.linkAndText().url
        loadNavgationController()?.pushViewController(sourseWVC, animated: true)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        backgroundColor = UIColor.whiteColor()
        //添加ui
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置ui
    private func setupUI() {
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(verified_type_image)
        addSubview(timeLabel)
        addSubview(sourceBtn)
        addSubview(contentLabel)
        //添加图片视图
        addSubview(pictureView)
        
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(StatusCellMarigin)
            make.size.equalTo(CGSize(width: StatusCellHeadImageWidth, height: StatusCellHeadImageWidth))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMarigin)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMarigin)
        }
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMarigin)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMarigin)
        }
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(nameLabel.snp_left)
        }
        sourceBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMarigin)
            make.height.equalTo(timeLabel.snp_height)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(iconView.snp_left)
        }
        //添加图片视图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(contentLabel.snp_left)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //添加头部视图的约束
        self.snp_makeConstraints { (make) -> Void in
            //记录底部的约束
           self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
        
        //设置btn的点击事件
        sourceBtn.addTarget(self, action: "didSourceBtnClick:", forControlEvents: .TouchUpInside)
    }
    
    //懒加载控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel:UILabel = UILabel(content: "下雨", color: UIColor.orangeColor(), fontSize: 14)
    private lazy var mbrankImage:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verified_type_image:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var timeLabel:UILabel = UILabel(content: "20:20", color: UIColor.orangeColor(), fontSize: 10)
//    private lazy var sourceLabel:UILabel = UILabel(content: "外星球", color: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceBtn:UIButton = UIButton(title: "", color: UIColor.orangeColor(), fontSize: 10)
    private lazy var contentLabel:UILabel = UILabel(content: "", color: UIColor.grayColor(), fontSize: 18, marigin: StatusCellMarigin)
   //懒加载图片视图
    private lazy var pictureView:PictureCellView = PictureCellView()
}
